package com.jscm.openselect;

import com.scm.all.export.ByteUtils;
import com.scm.all.export.SystemUtils;
import com.scm.all.export.WindowOperationUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据内容
 */
public class SelectTextType {
    public static ComboBox comboBox = new ComboBox();
    public static ObservableList<String> observableList = FXCollections.observableArrayList();
    static {
        init();
    }

    private static void init() {
        comboBox.setPrefWidth(280);
        comboBox.setPrefHeight(40);
        comboBox.setPromptText("选择类型在进行搜索默认4字节");
        comboBox.setFocusTraversable(false);
        observableList.addAll(
                "==================",
                "2字节字符型-小于",
                "2字节字符型-等于",
                "2字节字符型-大于",
                "2字节字符型-全部",
                "==================",
                "2字节短整数型-小于",
                "2字节短整数型-等于",
                "2字节短整数型-大于",
                "2字节短整数型-全部",
                "==================",
                "4字节整数型-小于",
                "4字节整数型-等于",
                "4字节整数型-大于",
                "4字节整数型-全部",
                "==================",
                "8字节长整数型-小于",
                "8字节长整数型-等于",
                "8字节长整数型-大于",
                "8字节长整数型-全部",
                "==================",
                "4字节浮点数型-小于",
                "4字节浮点数型-等于",
                "4字节浮点数型-大于",
                "4字节浮点数型-全部",
                "==================",
                "8字节双精度数型-小于",
                "8字节双精度数型-等于",
                "8字节双精度数型-大于",
                "8字节双精度数型-全部",
                "==================",
                "16进制字节数组",
                "16进制00??01特征码数组",
                "==================",
                "UTF8",
                "UniCode",
                "Ascii",
                "==================",
                "…………大于的数值",
                "…………小于的数值",
                "==================",
                "…………两数之间数值 0-2",
                "==================",
                "…………未改变的数值",
                "…………改变的数值",
                "…………过滤长度0值"
        );
        comboBox.setItems(observableList);
        comboBox.getSelectionModel().select(14);
        if(ByteUtils.sundayString((String) comboBox.getSelectionModel().getSelectedItem(),"==================").size()>0){
            SelecthBoxSearchBtn.button.setDisable(true);
            SelecthBoxSearchBtnTow.button.setDisable(true);
        }else {
            SelecthBoxSearchBtn.button.setDisable(false);
            SelecthBoxSearchBtnTow.button.setDisable(false);
        }
        comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(ByteUtils.sundayString((String) newValue,"==================").size()>0){
                SelecthBoxSearchBtn.button.setDisable(true);
                SelecthBoxSearchBtnTow.button.setDisable(true);
            }else {
                SelecthBoxSearchBtn.button.setDisable(false);
                SelecthBoxSearchBtnTow.button.setDisable(false);
            }
            System.out.println(SelectTextType.comboBox.getSelectionModel().getSelectedIndex());
            switch (SelectTextType.comboBox.getSelectionModel().getSelectedIndex()){
                case 1:
                case 2:
                case 3:
                case 4:

                case 6:
                case 7:
                case 8:
                case 9:

                case 11:
                case 12:
                case 13:
                case 14:

                case 16:
                case 17:
                case 18:
                case 19:

                case 21:
                case 22:
                case 23:
                case 24:

                case 26:
                case 27:
                case 28:
                case 29:

                case 31:

                case 32://不要填写

                case 34:
                case 35:
                case 36:SelectText.textField.setOpacity(1);SelectText.textField.setDisable(false);break;

                case 38:
                case 39:
                case 43:
                case 44:
                case 45:SelectText.textField.setOpacity(0);SelectText.textField.setDisable(true);break;
            }
        });

    }
    public static boolean TypeToBytes(){
        if(!StringUtils.equals(SelectText.textField.getText(),"")){
            try {
                switch (SelectTextType.comboBox.getSelectionModel().getSelectedIndex()){
                    case 1:
                    case 2:
                    case 3:
                    case 4:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText().charAt(0),"char");break;

                    case 6:
                    case 7:
                    case 8:
                    case 9:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"short");break;

                    case 11:
                    case 12:
                    case 13:
                    case 14:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"int");break;

                    case 16:
                    case 17:
                    case 18:
                    case 19:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"long");break;

                    case 21:
                    case 22:
                    case 23:
                    case 24:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"float");break;

                    case 26:
                    case 27:
                    case 28:
                    case 29:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"double");break;

                    case 31:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(ByteUtils.hexStrToBytes(SelectText.textField.getText()),"byte");break;

                    case 32://不要填写

                    case 34:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"StringUTF8");break;
                    case 35:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"StringUniCode");break;
                    case 36:SelecthBoxSearchBtn.data = ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"StringAscii");break;


                }
            }catch (NumberFormatException numberFormatException){
                SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"请选择正确的格式写入正确的数值","温馨提示",0);
                return false;
            }
            return true;
        }else {
            SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"不能为空","温馨提示",0);
            return false;
        }
    }
}
